package sample.util;

import com.alibaba.fastjson.JSON;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import org.apache.http.protocol.HTTP;
import sample.model.City;
import sample.model.ExtremeData;
import sample.model.StandardData;
import sample.model.User;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class Utils {

    public static List<StandardData> writeDataToTableColumn(List<Object> list, TableView<StandardData> tableView){

//        ObservableList columns = tableView.getColumns();
//        ObservableList<TableColumn> items = tableView.getItems();
        List<StandardData> standardDataList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String s = JSON.toJSONString(list.get(i));
            StandardData standardData = JSON.parseObject(s, StandardData.class);
            standardDataList.add(standardData);
        }
        ObservableList<StandardData> objects = FXCollections.observableArrayList(standardDataList);
//        for (StandardData standardData : list) {
//            objects.add(standardData)
//        }
        tableView.setItems(objects);
        return standardDataList;
    }

    public static void writeFilterDataToTableColumn(List<StandardData> list, TableView<StandardData> tableView){
        ObservableList<StandardData> objects = FXCollections.observableArrayList(list);
        tableView.setItems(objects);
        return;
    }



    public static List<ExtremeData> writeExtremeDataToTableColumn(List<Object> list, TableView<ExtremeData> tableView){
        List<ExtremeData> extremeDataList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String s = JSON.toJSONString(list.get(i));
            ExtremeData extremeData = JSON.parseObject(s, ExtremeData.class);
            extremeDataList.add(extremeData);
        }
//        ObservableList columns = tableView.getColumns();
        ObservableList<ExtremeData> objects = FXCollections.observableArrayList(extremeDataList);
        tableView.setItems(objects);
        return extremeDataList;
    }
    public static List<City> parseCityList(List<Object> list){
        List<City> cities = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String s = JSON.toJSONString(list.get(i));
            City city = JSON.parseObject(s, City.class);
            cities.add(city);
        }
        return cities;

    }
    public static String standardDataToJsonString(String url){

        List<StandardData> standardData = ParseExcel.parseStandardExcel(url);
        String s = JSON.toJSONString(standardData);
        System.out.println(s);
        try {
            s = URLEncoder.encode(s, HTTP.UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return s;
    }

    public static String extremeDataToJsonString(String url) {
        List<ExtremeData> extremeData = ParseExcel.parseExtremeExcel(url);
        String s = JSON.toJSONString(extremeData);
        System.out.println(s);
        try {
            s = URLEncoder.encode(s, HTTP.UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return s;
    }
    public static String userToJsonString(User user) {
        String s = JSON.toJSONString(user);
        System.out.println(s);
        try {
            s = URLEncoder.encode(s, HTTP.UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return s;
    }
}
